home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9707 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.5 KB  |  90 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Paging program
  5. Date: 12 Mar 1996 13:16:46 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4i4pjuINN72c@keats.ugrad.cs.ubc.ca>
  8. References: <Do5ABu.1v0@matt.fidalgo.net>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <Do5ABu.1v0@matt.fidalgo.net>,
  12. Matt Gischer <matt@matt.fidalgo.net> wrote:
  13.  >I'm trying to get this program here to work.  It's supposed to take a 
  14.  >file (from stdin or the command line) and pause every 24 lines.  
  15.  
  16. Boy, and I thought you were going to ask an off topic question about
  17. implemending a demand paging virtual memory scheme. Silly me...
  18.  
  19.  >(although i'd like some insight on how to find out how many rows the 
  20.  >users terminal has).  Anyway.. this thing prints out the last line of the 
  21.  
  22. There is a way of doing this in UNIX. First of all, you can query the LINES
  23. environment variable using getenv(). Secondly, you can retrive a special
  24. structure from the terminal driver that has the rows and columns info via a
  25. special ioctl().  The SIGWINCH signal can be delivered to your process when the
  26. terminal size changes, at which point you re-read the structure. But that is a
  27. subject for comp.unix.programmer...
  28.  
  29.  >file however many lines it is short of a full page on the last page.  
  30.  >So.. i was wondering if anyone could help me with that. Thanks.
  31.  >
  32.  >#include <stdio.h>
  33.  >
  34.  >main(int argc, char *argv[])
  35.  >{
  36.  >FILE *in;
  37.  >char t[80],enter;
  38.  >int x=0,z=0,s=0,y=0;
  39.  >if((in=fopen(argv[1], "r"))==NULL) {
  40.  >in=stdin;
  41.  >}
  42.  >while(!feof(in)) {
  43.  >fgets(t, 79,in);
  44.  >x++;
  45.  >}
  46.  >x--;
  47.  >rewind(in);
  48.  >y=x/24;
  49.  >if((y*24)<x) {
  50.  >y++;
  51.  >}
  52.  >for(s=0;s<y;s++) {
  53.  >    for(z=0;z<24;z++) {
  54.  >        fgets(t,79,in);
  55.  >        if(t!=NULL) {
  56.  >            print(t);
  57.  >        }
  58.  >    }
  59.  >    if(x>=24) {
  60.  >    printf("Hit enter for more");
  61.  >    enter=getchar();
  62.  >}
  63.  >}
  64.  >fclose(in);
  65.  >return 0;
  66.  >}
  67.  
  68. Boy, someone needs practice! How about:
  69.  
  70. int count = 0;
  71.  
  72. while(1) {
  73.     /* read line from file     */
  74.     /* if end of file, exit    */
  75.     if (++count > 23) {
  76.         count = 0;
  77.         /* prompt for enter    */
  78.     }
  79. }
  80.  
  81. Another way is to start with a counter of 1, and test its divisibility by 24
  82. using the modulo operator (%).
  83.  
  84. Do you realize that your if (t!=NULL) test will never succeed because the
  85. expression t gives the address of the first element of the t array, an address
  86. that never changes? Why are you using a zillion variables for such a simple
  87. 3roblem? You're not writing a compiler!
  88. -- 
  89.  
  90.